package com.boss.core.manage;

import hudson.CopyOnWrite;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * Environment section
 * @author Momin Noor Khan
 */
public class BossEnvironment implements Comparable<BossEnvironment>{
    private String environmentName;
    private boolean disabled;

    @CopyOnWrite
    private volatile List<BossServer> servers = new ArrayList<BossServer>();

    private transient int hashCode = 0;

    @DataBoundConstructor
    public BossEnvironment(String environmentName, boolean disabled, List<BossServer> servers) {
        this.environmentName = environmentName;
        this.disabled = disabled;
        this.servers = servers;
    }

    public String getEnvironmentName() {
        return environmentName;
    }

    public void setEnvironmentName(String environmentName){
        this.environmentName = environmentName;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled){
        this.disabled = disabled;
    }

    public List<BossServer> getServers() {
        return servers;
    }

    public void setServers(List<BossServer> servers){
        this.servers = servers;
    }

    public int compareTo(BossEnvironment otherEnvironment) {
        String envName = ((BossEnvironment) otherEnvironment).getEnvironmentName();
        return (this.getEnvironmentName()).compareTo(envName);
    }

    @Override
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }

        if(!(obj instanceof BossEnvironment)){
            return false;
        }

        BossEnvironment environment = (BossEnvironment)obj;

        if(!this.environmentName.equalsIgnoreCase(environment.getEnvironmentName())){
            return false;
        }
        return true;
    }

    @Override
    public int hashCode(){
        final int multiplier = 11;
        if(hashCode == 0){
            int code = 123;
            code = multiplier*code;
            code = multiplier*code + environmentName.hashCode();
            hashCode = code;
        }
        return hashCode;
    }
}
